/**
 * kl yy-ds @2021
 */
package com.cxps.server.auth;

import com.cxps.cloud.common.ConstantPool;
import com.cxps.cloud.common.CustomUser;
import com.cxps.cloud.utils.JwtTokenUtil;
import com.cxps.server.menu.service.MenuService;
import com.cxps.server.user.domain.UserEntity;
import com.cxps.server.user.service.UserService;
import com.cxps.server.vip.domain.VIPInfo;
import com.cxps.server.vip.service.VIPService;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.cxps.cloud.common.ConstantPool.R_USER_INFO;

/**
 * 登录认证逻辑层
 *
 * @author YouCai.Liu
 * @since 2021/10/24
 * ...
 */
@Service
public class AuthService {

    @Autowired
    private UserService userService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private VIPService vipService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 登录
     *
     * @param username 用户名
     * @param password 密码
     */
    public LoginVo login(String username, String password) {
        // 认证用户
        UserEntity entity = userService.loadUserByUsername(username);
        if (null == entity) {
            throw new IllegalArgumentException("用户不存在");
        }

        // 验证密码
        if (!passwordEncoder.matches(password, entity.getPassword())) {
            throw new IllegalArgumentException("密码错误");
        }

        // 构建自定义用户对象信息
        CustomUser user = new CustomUser();
        BeanUtils.copyProperties(entity, user);
        // 根据用户ID查询用户的权限集合
        List<String> userMenu = userService.getMenusByUserId(entity.getId());
        user.setAuthorities(userMenu);

        // 返回登录对象
        LoginVo loginVo = new LoginVo();
        loginVo.setUserName(entity.getUserName());
        loginVo.setUserImg(entity.getUserImg());
        loginVo.setToken(JwtTokenUtil.generateToken(user));
        // 查询所有的权限集合
        List<String> allMenu = menuService.getAllMenu();
        // 与用户所有拥有的权限集合做对比，进行标识
        Map<String, Boolean> authMap = new HashMap<>();
        for (String menu : allMenu) {
            authMap.put(menu, userMenu.contains(menu));
        }
        loginVo.setUserAuth(authMap);

        return loginVo;
    }

    /**
     * 登录
     *
     * @param username
     * @param password
     * @return
     */
    public Map<String, String> login2(String username, String password) {
        // 认证用户
        VIPInfo entity = vipService.loadUserByUsername(username);
        if (null == entity) {
            throw new IllegalArgumentException("用户不存在");
        }

        // 验证密码
        if (!passwordEncoder.matches(password, entity.getPassword())) {
            throw new IllegalArgumentException("密码错误");
        }

        String userName = entity.getVipName();
        if(StringUtils.isBlank(userName)){
            userName = entity.getVipPhone();
        }

        // 将用户登录的信息存储到Redis
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set(ConstantPool.R_USER_INFO + entity.getId(), userName);

        Map<String, String> map = new HashMap<>();
        map.put("token", entity.getId());
        map.put("userName", userName);
        return map;
    }
}
